package com.mz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mz.common.dto.GlobalException;
import com.mz.common.dto.PaginationVo;
import com.mz.dao.OmsGoodsCategoryDao;
import com.mz.entity.OmsGoodsCategory;
import com.mz.entity.vo.GoodCategoryVo;
import com.mz.service.OmsGoodsCategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * (OmsGoodsCategory)表服务实现类
 *
 * @author makejava
 * @since 2023-12-13 13:38:24
 */
@Service
public class OmsGoodsCategoryServiceImpl implements OmsGoodsCategoryService {

    @Resource
    private OmsGoodsCategoryDao omsGoodsCategoryDao;

    @Override
    public IPage getCategoryPagination(PaginationVo paginationVo) throws Exception {

        IPage page = new Page( paginationVo.getPageIndex(), paginationVo.getPageSize() );
        LambdaQueryWrapper<OmsGoodsCategory> lqs = new LambdaQueryWrapper<>();
        lqs.orderByDesc(OmsGoodsCategory::getCreateTime);
        omsGoodsCategoryDao.selectPage( page, lqs );
        return page;
    }

    @Override
    public List<OmsGoodsCategory> getCategoryAllList() throws Exception {

        List<OmsGoodsCategory> omsGoodsCategories = omsGoodsCategoryDao.selectList(null);
        return omsGoodsCategories;
    }

    @Override
    public void addGoodsCategory(GoodCategoryVo goodCategoryVo) throws Exception {

        if( goodCategoryVo.getId() != null ){
            throw GlobalException.gen(10021, "商品分类接口调用错误");
        }

        if( goodCategoryVo.getParentId() == null ){
            goodCategoryVo.setParentId(0L);
        }
        if( goodCategoryVo.getLevel() == null ){
            goodCategoryVo.setLevel(0);
        }
        if( goodCategoryVo.getSort() == null ){
            goodCategoryVo.setSort(0);
        }

        OmsGoodsCategory omsGoodsCategory = new OmsGoodsCategory();
        BeanUtils.copyProperties(goodCategoryVo, omsGoodsCategory);

        System.out.println(omsGoodsCategory);
        omsGoodsCategoryDao.insert(omsGoodsCategory);
    }

    @Override
    @Transactional
    public void updateGoodsCategory(GoodCategoryVo goodCategoryVo) throws Exception {

        OmsGoodsCategory omsGoodsCategory = new OmsGoodsCategory();
        omsGoodsCategory.setId(goodCategoryVo.getId())
                .setName(goodCategoryVo.getName())
                .setSort(goodCategoryVo.getSort());
        omsGoodsCategoryDao.updateById(omsGoodsCategory);
    }

    @Override
    public void removeGoodsCategory(List<Long> idList) throws Exception {

        if( idList == null || idList.size() == 0 ){
            throw GlobalException.gen(10022, "要删除的商品分类不存在");
        }
        omsGoodsCategoryDao.deleteBatchIds(idList);
    }
}

